package com.leetcode.problems024;


import com.leetcode.util.ListNode;

/**
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode dummyHead = new ListNode(0);
        ListNode preNode = dummyHead;
        ListNode curNode = head;
        ListNode nextNode = null;

        while(curNode != null && curNode.next != null) {
            nextNode = curNode.next;

            preNode.next = nextNode;
            ListNode nextNextNode = nextNode.next;
            nextNode.next = curNode;
            curNode.next = nextNextNode;

            preNode = curNode;
            curNode = curNode.next;
        }
        ListNode retNode = dummyHead.next;
        dummyHead.next = null;
        return retNode;

    }
    public static void main(String[] args) {
        int[] arr = {1,2};
        ListNode.printListNode(new Solution().swapPairs(ListNode.getListFormArray(arr)));
    }
}
